package com.pig.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.pig.dao.CommentDao;
import com.pig.exception.UnExceptDataException;
import com.pig.pojo.goods.Comment;
import com.pig.service.goods.CommentService;
import com.pig.util.PageObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service(timeout = 1200000)
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentDao commentDao;

    @Override
    public PageObject<Comment> findCommentByGoodsId(Integer goodsId, Integer pageCurrent) {
        int pageSize = 5;
        List<Comment> comm = commentDao.findCommentByGoodsId(goodsId, (pageCurrent - 1) * pageSize, pageSize);
        return new PageObject<>(pageSize,comm.size(),pageCurrent,comm);
    }

    @Override
    public int insertComment(Comment comment) {
        if (StringUtils.isEmpty(comment.getComm())) {
            throw new UnExceptDataException("请填写评论！");
        }
        if (StringUtils.isEmpty(comment.getUsername())) {
            throw new UnExceptDataException("用户不可为空！");
        }
        if (StringUtils.isEmpty(comment.getGoodsId())) {
            throw new UnExceptDataException("商品不可为空!");
        }
        comment.setCreatedTime(new Date());
        return commentDao.insert(comment);
    }
}
